package com.px.lovepath.ui.home;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.px.lovepath.R;
import com.px.lovepath.domain.config.ConfigInfo;
import com.px.lovepath.ui.general.ViewController;
import com.px.lovepath.ui.general.pic.AvatarView;
import com.px.lovepath.ui.home.HomeListView.OnPositionChangedListener;
import com.px.lovepath.ui.home.LoveMessage.Path;
import com.px.lovepath.ui.home.MenuController.MenuChangedListener;
import com.px.lovepath.utils.DateUtil;
import com.px.lovepath.utils.Provider;

public class ContentController extends ViewController implements MenuChangedListener, OnPositionChangedListener  {

	private HomeListView mListView;
	private HomeAdapter mAdapter;
	private ArrayList<LoveMessage> mMessagesList;
	private LayoutInflater mInflater;
	private float[] lastTime = { 0f, 0f };
	private FrameLayout mClockLayout;
	private int mSelect;
	
	public ContentController(Activity parentActivity, View view) {
		super(parentActivity, view);
		
		MenuController menuController = new MenuController(mParentActivity, findViewById(R.id.home__home_menu_view__root), this);
		View setting = findViewById(R.id.home__home_view__edit);
		setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mParentActivity, SettingActivity.class);
				mParentActivity.startActivityForResult(intent, 0);
			}
		});
		mInflater = LayoutInflater.from(mParentActivity);
		mClockLayout = (FrameLayout) findViewById(R.id.home__home_clock_view);
		mListView = (HomeListView) findViewById(R.id.home__home_view__list);
		mAdapter = new HomeAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnPositionChangedListener(this);
		mSelect = ConfigInfo.get().getHomeSelect();
		loadData();
	}
	
	// ### 方法 ###
	public void loadData() {
//		mMessagesList.clear();
//		mMessagesList.addAll(Provider.getCallRecords(mParentActivity));
//		mAdapter.notifyDataSetChanged();
	}
	
	// ### 实现接口 ###
	@Override
	public void onMenuChanged(int selected) {
//		mSelect = selected;
//		ConfigInfo.get().setHomeSelect(mSelect);
//		mAdapter.notifyDataSetChanged();
	}
	@Override
	public void onPositionChanged(HomeListView listView, int firstVisiblePosition, View scrollBarPanel) {
		LoveMessage message = (LoveMessage) mAdapter.getItem(firstVisiblePosition);
		Date date = message.getDate();
		TextView datestr = (TextView) findViewById(R.id.clock_digital_date);
		datestr.setText(DateUtil.getPrefixTimeString(date));
		TextView timeStr = (TextView) findViewById(R.id.clock_digital_time);
		StringBuffer temp = new StringBuffer();
		int hour = date.getHours();
		int minute = date.getMinutes();
		temp.append(hour > 12 ? hour - 12 : hour);
		temp.append(":").append(minute);
		timeStr.setText(temp.toString());
		RotateAnimation[] tmp = computeAni(minute, hour);
		ImageView minView = (ImageView) findViewById(R.id.clock_face_minute);
		minView.startAnimation(tmp[0]);
		ImageView hourView = (ImageView) findViewById(R.id.clock_face_hour);
		hourView.setImageResource(R.drawable.clock_hour_rotatable);
		hourView.startAnimation(tmp[1]);
	}
	@Override
	public void onScollPositionChanged(View scrollBarPanel, int top) {
		MarginLayoutParams layoutParams = (MarginLayoutParams) mClockLayout.getLayoutParams();
		layoutParams.setMargins(0, top, 0, 0);
		mClockLayout.setLayoutParams(layoutParams);
	}

	// #### 实现  ###
	private float[] computMinAndHour(int currentMinute, int currentHour) {
		float minuteRadian = 6f * currentMinute;
		float hourRadian = 360f / 12f * currentHour;
		float[] rtn = new float[2];
		rtn[0] = minuteRadian;
		rtn[1] = hourRadian;
		return rtn;
	}
	private RotateAnimation[] computeAni(int min, int hour) {

		RotateAnimation[] rtnAni = new RotateAnimation[2];
		float[] timef = computMinAndHour(min, hour);
		// AnimationSet as = new AnimationSet(true);
		// 创建RotateAnimation对象
		// 0--图片从哪开始旋转
		// 360--图片旋转多少度
		// Animation.RELATIVE_TO_PARENT, 0f,// 定义图片旋转X轴的类型和坐标
		// Animation.RELATIVE_TO_PARENT, 0f);// 定义图片旋转Y轴的类型和坐标
		RotateAnimation ra = new RotateAnimation(lastTime[0], timef[0], Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setFillAfter(true);
		ra.setFillBefore(true);
		// 设置动画的执行时间
		ra.setDuration(800);
		// 将RotateAnimation对象添加到AnimationSet
		// as.addAnimation(ra);
		// 将动画使用到ImageView
		rtnAni[0] = ra;
		lastTime[0] = timef[0];
		// AnimationSet as2 = new AnimationSet(true);
		// 创建RotateAnimation对象
		// 0--图片从哪开始旋转
		// 360--图片旋转多少度
		// Animation.RELATIVE_TO_PARENT, 0f,// 定义图片旋转X轴的类型和坐标
		// Animation.RELATIVE_TO_PARENT, 0f);// 定义图片旋转Y轴的类型和坐标
		RotateAnimation ra2 = new RotateAnimation(lastTime[1], timef[1], Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);

		// 设置动画的执行时间
		ra2.setFillAfter(true);
		ra2.setFillBefore(true);
		ra2.setDuration(800);
		// 将RotateAnimation对象添加到AnimationSet
		// as2.addAnimation(ra2);
		// 将动画使用到ImageView
		rtnAni[1] = ra2;
		lastTime[1] = timef[1];
		return rtnAni;
	}
	
	// ### 内部类 ###
	private class HomeAdapter extends BaseAdapter {
		
		@Override
		public int getCount() {
			return mMessagesList == null ? 0 : mMessagesList.size();
		}

		@Override
		public Object getItem(int position) {
			return mMessagesList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.home__home_list_item_left_view, null);
			}
			LoveMessage loveMessage = (LoveMessage) getItem(position);
			AvatarView avatar = (AvatarView) convertView.findViewById(R.id.home__home_list_item_view__avatar);
			TextView name = (TextView) convertView.findViewById(R.id.home__home_list_item_view__name);
			
			if (loveMessage.getPath() == Path.TOME) {
				name.setText(ConfigInfo.get().getTaName());
			} else {
				name.setText(ConfigInfo.get().getUserName());
			}
			
			LinearLayout bodyGroup = (LinearLayout) convertView.findViewById(R.id.feed_post_body);
			bodyGroup.removeAllViews();
			View body = mInflater.inflate(R.layout.home__home_list_item_call_view, null);
			TextView message = (TextView) body.findViewById(R.id.thought_main);
			message.setText(loveMessage.getMessage());
			bodyGroup.addView(body);
			return convertView;
		}

	}
}
